﻿from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from win32com.client import Dispatch
import os
import time

class _CpBase(object):
    """Базовый класс для страниц"""

    __WAIT_TIME__ = 20

    def __init__(self, driver):
        self.driver = driver

    def open(self, driver, url):
        self.driver.get(url)

    def presence_of_element_located(self, Locator):
        wait = WebDriverWait(self.driver, self.__WAIT_TIME__)
        element = wait.until(EC.presence_of_element_located((By.ID,Locator)))

    def presence_of_element_located_byXpath(self, Locator):
        wait = WebDriverWait(self.driver, self.__WAIT_TIME__)
        element = wait.until(EC.presence_of_element_located((By.XPATH,Locator)))

    def presence_of_element_located_byLinkText(self, Locator):
        wait = WebDriverWait(self.driver, self.__WAIT_TIME__)
        element = wait.until(EC.presence_of_element_located((By.LINK_TEXT,Locator)))

    #Для алертов
    def wait_for_alert(self):
        wait = WebDriverWait(self.driver, self.__WAIT_TIME__)
        wait.until(EC.alert_is_present())

    def assert_url(self, url):
        current_url = self.driver.current_url
        print "Открылся url: ", current_url
        assert current_url == url

    def text_to_be_present_in_element(self, Locator, Text):
        wait = WebDriverWait(self.driver, self.__WAIT_TIME__)
        element = wait.until(EC.text_to_be_present_in_element((By.CSS_SELECTOR, Locator), Text))

    def submit_certificate_window(self):
        autoit = Dispatch('AutoItX3.Control')
        title = "Безопасность Windows"
        handle = autoit.WinGetHandle(title.decode('utf-8'))
        print "Handle окна: ", handle
        #autoit.WinClose(title.decode('utf-8'))
        time.sleep(5)
        autoit.ControlClick(title.decode('utf-8'), "", "[CLASS:Button; INSTANCE:2]")

    def sign_cryptopro(self):
        #Подождать появления окна
        self.presence_of_element_located_byXpath(".//*[text()='Выбор сертификата для подписания']")
        self.driver.find_element_by_xpath(".//*[@class='x-grid3-row  x-grid3-row-first x-grid3-row-last']").click()
        self.driver.find_element_by_xpath(".//*[@type='button' and text()='Выбрать']").click()

""" Expected Conditions:

    title_is
    title_contains
    presence_of_element_located
    visibility_of_element_located
    visibility_of
    presence_of_all_elements_located
    text_to_be_present_in_element
    text_to_be_present_in_element_value
    frame_to_be_available_and_switch_to_it
    invisibility_of_element_located
    element_to_be_clickable - it is Displayed and Enabled.
    staleness_of
    element_to_be_selected
    element_located_to_be_selected
    element_selection_state_to_be
    element_located_selection_state_to_be
    alert_is_present
"""

"""
    def fill_form_by_css(self, form_css, value):
          elem = self.driver.find(form_css)
          elem.send_keys(value)

    def fill_form_by_id(self, form_element_id, value):
        elem = driver.find_element_by_id
      
    def fill_form_by_id(self, form_element_id, value):
          return self.fill_form_by_css('#%s' % form_element_id, value)

    def fill_form_by_name(self, form_element_name, value):
          return self.fill_form_by_name('#%s' % form_element_name, value)

    def fill_form_by_xpath(self, form_element_xpath, value):
          return self.fill_form_by_xpath('#%s' % form_element_xpath, value)
"""